package com.omesoft.hksnore.util.omeview;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.omesoft.hksnore.R;


/**
 * Created by Omesoft on 2016/11/17.
 */

public class MyDialog extends Dialog {

    public MyDialog(Context context, int theme) {
        super(context, theme);
    }

    public void setMessage(String message) {

    }

    /**
     * Creare the custom dialog
     */
    public void updateTitleData(String titleData) {
        if (titleData != null) {

        }
    }

    /**
     * Helper class for creating a custom dialog
     */
    public static class Builder {
        private Context context;
        private String title;
        private String myViewTitle;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private View view;
        private Boolean isShowLine;
        private TextView textView;
        private static View layout;
        private OnClickListener positiveButtonClicklistener, negativeButtonClicklistener;

        public Builder(Context context) {
            this.context = context;
        }

        public static void updateMessage(String message) {

        }

        /**
         * Set the Dialog message from String
         *
         * @param
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }


        /**
         * Set the Dialog message from resource
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * 设置自定义View时候的标题
         */
        public Builder setMyViewTitle(String myViewTitle) {
            this.myViewTitle = myViewTitle;
            return this;
        }

        /**
         * Set a custom content view for the Dialog. If a message is set, the
         * contentView is not added to the Dialog...
         *
         * @param v
         * @return
         */
        public Builder setView(View v) {
            this.view = v;
            return this;
        }

        public Builder isShowLine(Boolean isShowLine) {
            this.isShowLine = isShowLine;
            return this;
        }


        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         * @Param listener
         */

        public Builder setPositiveButton(int positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = (String) context.getText(positiveButtonText);
            this.positiveButtonClicklistener = listener;
            return this;

        }

        /**
         * Set the positive button text and it's listener
         *
         * @param positiveButtonText
         * @param listener
         * @return
         */
        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClicklistener = listener;
            return this;
        }

        /**
         * Set the negative button resource and it's listener
         *
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClicklistener = listener;
            return this;
        }

        /**
         * Set the negative button text and it's listener
         *
         * @param negativeButtonText
         * @param listener
         * @return
         */
        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClicklistener = listener;
            return this;
        }

        public MyDialog create() {
            LayoutInflater inflater = LayoutInflater.from(context);
            final MyDialog dialog = new MyDialog(context, R.style.myDialog);
            layout = inflater.inflate(R.layout.my_dialog, null);
            dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            //set the dialog title
            if (title != null) {
                ((TextView) layout.findViewById(R.id.dialog_title)).setText(title);
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.dialog_title).setVisibility(View.GONE);
            }
            //set the content message
            if (message != null) {
                ((TextView) layout.findViewById(R.id.dialog_message)).setText(message);
            } else {
                ((TextView) layout.findViewById(R.id.dialog_message)).setVisibility(View.GONE);
            }

            // 设置自定义View显示时候的tiele;
            if (myViewTitle != null) {
                ((TextView) layout.findViewById(R.id.custom_title)).setText(myViewTitle);
            } else {
                ((TextView) layout.findViewById(R.id.custom_title)).setVisibility(View.GONE);
            }


            if (view != null) {
                layout.findViewById(R.id.dialog_topPanel).setVisibility(View.GONE);
                layout.findViewById(R.id.dialog_customPanel).setVisibility(View.VISIBLE);

                // if no message set,add the contentView to the dialog bady

//                layout.findViewById(R.id.dialog_custom).setBackgroundDrawable(null);
                ((LinearLayout) layout.findViewById(R.id.dialog_custom)).removeAllViews();
                textView = (TextView) layout.findViewById(R.id.showline);
                LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.MATCH_PARENT);

                ((LinearLayout) layout.findViewById(R.id.dialog_custom)).addView(view, mLayoutParams);
                if (isShowLine == false) {
                    textView.setVisibility(View.GONE);
                }

            } else {
                layout.findViewById(R.id.dialog_customPanel).setVisibility(View.GONE);
            }
            if (positiveButtonText != null) {
                ((Button) layout.findViewById(R.id.positiveButton)).setText(positiveButtonText);
                if (positiveButtonClicklistener != null) {
                    ((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            positiveButtonClicklistener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }
            } else {
                layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
            }
            //set the cancel button

            if (negativeButtonText != null) {
                ((Button) layout.findViewById(R.id.negativeButton)).setText(negativeButtonText);
                if (negativeButtonClicklistener != null) {
                    ((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            negativeButtonClicklistener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }
            } else {
                //if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
            }
            dialog.setContentView(layout);
            dialog.setCanceledOnTouchOutside(true);
            return dialog;
        }

        public MyDialog show() {
            MyDialog dialog = create();
            dialog.show();
            return dialog;
        }

        //
        public static MyDialog getCustomDialog(Activity activity, String title,
                                               String Content, String leftBtn, String tightBtn,
                                               OnClickListener leftListener,
                                               OnClickListener rightListenner) {
            MyDialog.Builder builder = new MyDialog.Builder(activity);
            return builder.create();
        }

    }
}
